require.config({
	paths:{
		"cookie":"jquery.cookie",
		"jquery":"jquery-1.11.3",
		"fly":"jquery.fly"
	}
})
require(["cookie","jquery","fly"],function(cookie,jquery,fly){
//	图片切换部分
	$(function(){
		var $proSmall = $(".pro-album-small img");
//			$proSmall.each(function(i){
//				$(this).click(function(){
//					$(".big-pic img").attr("src","../img/detailimg/album-big-" + ++i + ".jpg");
//					$(".bigCursor img").attr("src","../img/detailimg/album-big-" + ++i + ".jpg");
//				})
//			})
		$proSmall.click(function(){
			var i = $proSmall.index($(this));
			$(".big-pic img").attr("src","../img/detailimg/album-big-" + ++i + ".jpg");	
			$(".bigCursor img").attr("src","../img/detailimg/album-big-" + i + ".jpg");
		})

		
		
	})
//	放大镜部分
	$(function(){
		var $smallImg = $(".big-pic"); //小图
		var $bigImg = $(".bigCursor img"); //大图
		var $smallCursor = $(".smallCursor"); //小可视区域
		var $bigCursor = $(".bigCursor"); //大可视区域
		//根据比例重新计算小可视区的大小
		//公式 ： 小可视区宽度 / 大可视宽度 = 小图宽度 / 大图宽度
		//alert($smallImg.outerWidth());//540
		//alert($bigCursor.outerWidth());//600
		$smallCursor.width($smallImg.outerWidth() * $bigCursor.outerWidth() / 4320);
		$smallCursor.height($smallImg.outerHeight() * $bigCursor.outerHeight() /1920);
		//由于小可视区域隐藏，因此不能使用offsetWidth属性来计算宽度。
		//smallCursor的边长
		diameter = $smallCursor.outerWidth();//75px;
		//大小可视区比例
		var scale = $bigCursor.outerWidth() / diameter / 10;
		$smallImg.mousemove(function(evt){
			$bigCursor.css("display","block");
			//鼠标坐标值
			var disX = evt.clientX - $smallImg.offset().left - diameter / 2;
			var disY = evt.clientY - $smallImg.offset().top - diameter / 2;
			//alert($smallImg.offset().top);
			//设置边界
			var leftSide = $smallImg.offset().left + diameter / 2;
			var topSide = $smallImg.offset().top + diameter / 2;
			var rightSide = $smallImg.offset().left + $smallImg.outerWidth() - diameter / 2;
			var downSide = $smallImg.offset().top + $smallImg.outerHeight() - diameter / 2;
			if(evt.pageX >= leftSide && evt.pageX <= rightSide && evt.pageY >= topSide && evt.pageY <= downSide){
				$smallCursor.css("display","block");
				//小图的位置
				$smallCursor.css("left",disX);
				$smallCursor.css("top",disY);
				//大图的位置
				$bigImg.css("left",-disX * scale);
				$bigImg.css("top",-disY * scale);
			}else{
				$smallCursor.css("display","none");
				$bigCursor.css("display","none");
			}
		})
		
	})
	$(function(){
		//加载已有购物车信息
		loadCart();
		//给购物车加点击事件
		$(".cart").click(function(){
			location.href = "cart.html";
		})
		//给加入购物车添加点击事件
		$(".btn-sub").click(function(e){
			var goodId = $(".info-title span").html();//不加参数是获取
			//alert(goodId);
			var goodinfo = $(".jqgoodInfo").html();
			//alert(goodinfo);
			var goodName = $(".info-title b").html();
			var goodPrice = $(".price-num").html();
			//alert(goodPrice);
			var goodSrc = $(".big-pic img").attr("src");
			//alert(goodSrc);
			var cartStr = $.cookie("cart") ? $.cookie("cart") : "";
			var cartObj = convertCartStrToObj(cartStr);
			if(goodId in cartObj){
				cartObj[goodId].num ++;
			}else{
				cartObj[goodId] = {
					name : goodName,
					price : goodPrice,
					num : 1,
					src : goodSrc,
					info : goodinfo
				};
			}
//			将新的购物车信息存回cookie
//			将对象转为字符串
			cartStr = convertObjToCartStr(cartObj);
			$.cookie("cart",cartStr,{expires:7,path:"/"});
			
		});
	});
	function convertCartStrToObj(cartStr){
		if(!cartStr){
			return{};
		}
		var goods = cartStr.split(":");
		var obj = {};
		for(var i = 0; i < goods.length; i ++){
			var data = goods[i].split(",");
			obj[data[0]] = {
				name : data[1],
				price : data[2],
				num : data[3],
				src : data[4],
				info : data[5]
			}
		}
		return obj;
	}
	function convertObjToCartStr(obj){
		var cartStr = "";
//		遍历对象
		for(var id in obj){
			if(cartStr){
				cartStr += ":";
			}
			cartStr += id + "," + obj[id].name + "," + obj[id].price + "," + obj[id].num + "," + obj[id].src + "," + obj[id].info;
 		}
		return cartStr;
	}
	function loadCart(){
		var cartStr = $.cookie("cart") ? $.cookie("cart") : "";
		var cartObj = convertCartStrToObj(cartStr);
		//获取到购物车中所有商品的数量
		var total = 0;
		for(var id in cartObj){
			total += cartObj[id].num;
		}
		$("cart-title").empty().append("购物车(" + total + ")");
	}
})























